package com.fxp.project.jiuselu.web.task;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.jiuselu.commons.utils.DateUtils;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.models.carinfo.entity.CarInfo;
import com.fxp.project.jiuselu.web.models.carinfo.mapper.CarInfoMapper;
import com.fxp.project.jiuselu.web.models.order.entity.JslOrder;
import com.fxp.project.jiuselu.web.models.order.entity.SplitOrder;
import com.fxp.project.jiuselu.web.models.order.mapper.JslOrderMapper;
import com.fxp.project.jiuselu.web.models.order.service.IJslOrderService;
import com.fxp.project.jiuselu.web.models.order.service.ISplitOrderService;
import com.fxp.project.jiuselu.web.models.order.vo.JslOrderVo;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: MyTask
 * @Description: 定时任务
 * @Date: 2021/5/13 14:50
 */
@Configuration
@EnableScheduling
public class MyTask {
    @Autowired
    private IJslOrderService orderService;

    @Autowired
    private ISplitOrderService splitOrderService;
    @Autowired
    private JslOrderMapper jslOrderMapper;

    @Autowired
    private IJslUserService jslUserService;

    @Autowired
    private CarInfoMapper carInfoMapper;



    /**
     * 十四天自动确认收货（每天的0点、13点、18点、21点都执行一次）
     */
    @Scheduled(cron = "0 0 0,13,18,21 * * ?")
//    @Scheduled(cron = "0 */2 * * * ?")
    public void autoReceive() {

        System.out.println("我启动了");
        List<JslOrder> list = orderService.list();
        for (JslOrder order : list) {
            if(order.getLogisticStatus()==1 && order.getOrderStatus()==0){
                 SplitOrder splitOrder = splitOrderService.selectSplitOrderByOrderId(order.getOrderId());
                if(splitOrder!=null){
                    long isDelayTime = splitOrder.getCreateTime().toEpochSecond(ZoneOffset.of("+8")) + 604800 - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
                    //判断是否点击延长收货
                    if (order.getIsDelay() == 1) {
                        isDelayTime = isDelayTime + 604800;
                        //判断是否已超过延长收货时间,超过则修改订单状态
                        if(isDelayTime <= 0){
                            JslOrder jslOrder1 = new JslOrder();
                            order.setOrderStatus(1);
                            order.setIsDelay(2);
                            order.setConfirmTime(LocalDateTime.now());
                            jslOrderMapper.updateById(order);

                            //添加佣金
                            jslUserService.calculateProfit(order.getOrderId(), order.getUserId(), new BigDecimal("0"));
                            //添加用户消费金额
                            JslUser jslUser = jslUserService.getById(order.getUserId());
                            jslUser.setConsumeAmount(jslUser.getConsumeAmount().add(order.getTotalAmount()));
                            jslUserService.updateById(jslUser);
                        }
                    }
                    //判断是否已超过延长收货时间,超过则修改订单状态
                    if(isDelayTime <= 0){
                        order.setOrderStatus(1);
                        order.setConfirmTime(LocalDateTime.now());
                        jslOrderMapper.updateById(order);

                        //添加佣金
                        jslUserService.calculateProfit(order.getOrderId(), order.getUserId(), new BigDecimal("0"));
                        //添加用户消费金额
                        JslUser jslUser = jslUserService.getById(order.getUserId());
                        jslUser.setConsumeAmount(jslUser.getConsumeAmount().add(order.getTotalAmount()));
                        jslUserService.updateById(jslUser);
                    }

                    System.out.println(isDelayTime);
                }else{
                    //判断车辆信息情况
                    QueryWrapper<CarInfo> wrapper = new QueryWrapper<>();
                    wrapper.eq("order_id",order.getOrderId());
                    CarInfo carInfo = carInfoMapper.selectOne(wrapper);
                    if(carInfo!=null){
                        long isDelayCarTime = carInfo.getCreateTime().toEpochSecond(ZoneOffset.of("+8")) + 604800 - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
                        if (order.getIsDelay() == 1) {
                            isDelayCarTime = isDelayCarTime + 604800;
                            //判断是否已超过延长收货时间,超过则修改订单状态
                            if(isDelayCarTime <= 0){
                                JslOrder jslOrder1 = new JslOrder();
                                order.setOrderStatus(1);
                                order.setIsDelay(2);
                                order.setConfirmTime(LocalDateTime.now());
                                jslOrderMapper.updateById(order);

                                //添加佣金
                                jslUserService.calculateProfit(order.getOrderId(), order.getUserId(), new BigDecimal("0"));
                                //添加用户消费金额
                                JslUser jslUser = jslUserService.getById(order.getUserId());
                                jslUser.setConsumeAmount(jslUser.getConsumeAmount().add(order.getTotalAmount()));
                                jslUserService.updateById(jslUser);
                            }
                        }
                        //判断是否已超过延长收货时间,超过则修改订单状态
                        if(isDelayCarTime <= 0){
                            order.setOrderStatus(1);
                            order.setConfirmTime(LocalDateTime.now());
                            jslOrderMapper.updateById(order);

                            //添加佣金
                            jslUserService.calculateProfit(order.getOrderId(), order.getUserId(), new BigDecimal("0"));
                            //添加用户消费金额
                            JslUser jslUser = jslUserService.getById(order.getUserId());
                            jslUser.setConsumeAmount(jslUser.getConsumeAmount().add(order.getTotalAmount()));
                            jslUserService.updateById(jslUser);
                        }

                    }
                }
            }
        }
    }

}
